<template>
    <div class="container">
         <el-form :model="ruleForm" ref="ruleForm" :rules="rules" :label-position="labelPosition" label-width="120px" class="demo-ruleForm">
            <el-row class="container">
                <el-col :span="12">
                    <el-form-item label="物料类型：" >

						<el-select v-model="ruleForm.materialType" placeholder="请选择" style="width:300px" >
							<el-option
							v-for="item in options"
							:key="item.value"
							:label="item.label"
							:value="item.value">
							</el-option>
						</el-select>
                    </el-form-item>
                    <el-form-item label="物料代码：" >
                        <el-input placeholder="请输入内容" v-model="ruleForm.materialCode"  style="width:300px"></el-input>
                    </el-form-item>
                 </el-col>
                <el-col :span="10">
                    <el-form-item label="物料名称：" >
                        <el-input placeholder="请输入内容" v-model="ruleForm.materialName" style="width:300px"></el-input>
                    </el-form-item>
                    <el-form-item label="规格：" style=" margin-top:30px;" v-if="ruleForm.materialType == '成品'">
						<el-input placeholder="请输入内容" v-model="ruleForm.materialSpecification"  style="width:300px;">
							<template slot="append">
								<el-select v-model="ruleForm.unit" placeholder="单位" style="width:100px">
								<el-option
								v-for="item in unitList"
								:key="item.id"
								:label="item.name"
								:value="item.name">
								</el-option>
							</el-select>
							</template>
						</el-input>

                    </el-form-item>
                </el-col>
            </el-row>

			<el-form-item class="footerFixed" label-width="0px">
				<el-button type="primary" @click="submitrule('ruleForm')">提交</el-button>
			    <el-button @click="backPage">返回</el-button>
			</el-form-item>
         </el-form>
    </div>
</template>

<script>
	import {unit_findByList,} from "@/request/api"
	import {materialManagement_add,materialManagement_select,materialManagement_update} from "@/request/produceApi"
    export default {
        data(){
            return{
				options: [],
                value: '',
				unitList:[],  //单位列表
                dialogTableVisible:false,
                ruleForm:{},
                rules:{},
                labelPosition:'right',
            }
        },
        mounted(){
          var lj = localStorage.getItem('lj');
          if( lj == "生产") {
            this.options = [
              {
                value: '原料',
                label: '原料'
              }, {
                value: '辅料',
                label: '辅料'
              }, {
                value: '半成品',
                label: '半成品'
              }, {
                value: '成品',
                label: '成品'
              }
            ]
          }else{
            this.options = [
              {
                value: '半成品',
                label: '半成品'
              }, {
                value: '成品',
                label: '成品'
              }
            ]
          }
			let id = this.$route.query.id
			if(id){
				let ids = {
					id: id
				}
				materialManagement_select(ids).then((res) => {
					this.ruleForm = res.data
					this.init()
					// console.log(res);
				})
			} else {
				this.init()
			}
        },
        methods:{
			//新增物料
			submitrule(){
				this.$confirm('确定要新增吗？', '提示', {
				    confirmButtonText: '确定',
				    cancelButtonText: '取消',
				    type: 'warning'
				}).then(() => {
					// var obj = {
					// Y: new Date().getFullYear(),
					// M: new Date().getMonth() + 1,
					// D: new Date().getDate(),
					// H: new Date().getHours(),
					// Mi: new Date().getMinutes(),
					// S: new Date().getSeconds()
					// }
					// if(obj.H < 10) obj.H = '0' + obj.H
					// if(obj.Mi < 10) obj.Mi = '0' + obj.Mi
					// if(obj.S < 10) obj.S = '0' + obj.S
					// var time = ' ' + obj.H + ':' + obj.Mi + ':' + obj.S
					// var times = obj.Y + '-' + obj.M + '-' + obj.D + time;

					let formdata = {
						materialManagement: {}
					}
					formdata.materialManagement.user_id = JSON.parse(localStorage.getItem('userid')),
					formdata.materialManagement.company_id = localStorage.getItem('companyId'),
					// formdata.materialManagement.createtime = times,
					formdata.materialManagement.materialType = this.ruleForm.materialType ,
					formdata.materialManagement.materialName = this.ruleForm.materialName ,
					formdata.materialManagement.materialCode = this.ruleForm.materialCode ,
					formdata.materialManagement.materialSpecification = this.ruleForm.materialSpecification ,
					formdata.materialManagement.unit = this.ruleForm.unit
					if(this.ruleForm.id){
						console.log(1111)
						formdata.materialManagement.id = this.ruleForm.id,
						materialManagement_update(formdata).then((res) =>{
							this.$notify({
						    title: '提示',
						    message: '修改成功',
						    type: 'success',
						    duration:1000
						});
						this.$router.go(-1)
						})
					} else {
						// console.log(222)
						// formdata.materialManagement.id = '',
						// console.log(11)
				    	materialManagement_add(formdata).then((res)=>{
							// consople.log(111)
							this.$notify({
								title: '提示',
								message: '新增成功',
								type: 'success',
								duration:1000
							});
							this.backPage()
						})
					}
				})
			},
			backPage(){
				console.log(222)
				this.$router.go(-1)
			},
            init(){
				//获取单位列表
				unit_findByList('').then((res) => {
				        this.unitList = res.data;
				});
            },
        }
    }
</script>

<style scoped>
 .el-pagination{
    text-align: right;
}

</style>
